package com.xiaoxu.spzx.manager.controller;

/**
 * ClassName:IndexController
 * Description:
 *
 * @author: 徐廷泉
 * @Create 2024/1/14 10:34
 */

import com.xiaoxu.spzx.manager.service.SysMenuService;
import com.xiaoxu.spzx.manager.service.SysUserService;
import com.xiaoxu.spzx.manager.service.ValidateCodeService;
import com.xiaoxu.spzx.model.dto.system.LoginDto;
import com.xiaoxu.spzx.model.entity.system.SysUser;
import com.xiaoxu.spzx.model.vo.common.Result;
import com.xiaoxu.spzx.model.vo.common.ResultCodeEnum;
import com.xiaoxu.spzx.model.vo.system.LoginVo;
import com.xiaoxu.spzx.model.vo.system.SysMenuVo;
import com.xiaoxu.spzx.model.vo.system.ValidateCodeVo;
import com.xiaoxu.spzx.utils.AuthContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户接口")
@RestController
@RequestMapping(value = "/admin/system/index")
public class IndexController {


    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ValidateCodeService validateCodeService;

    @Autowired
    private SysMenuService sysMenuService;

    //生成动态菜单
    @GetMapping("/menus")
    public Result menus() {
        List<SysMenuVo> sysMenuVoList =  sysMenuService.findUserMenuList() ;
        return Result.build(sysMenuVoList , ResultCodeEnum.SUCCESS) ;
    }




    //用户登录的方法
    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result login( @RequestBody LoginDto loginDto){


      LoginVo loginVo  =   sysUserService.login(loginDto);

      return Result.build(loginVo,ResultCodeEnum.SUCCESS);

    }
    /*
    * 生成验证码的方法
    * */
    @Operation(summary = "生成验证码的方法")
    @GetMapping(value = "/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo , ResultCodeEnum.SUCCESS) ;
    }

//    /*
//    * 获取用户信息
//    *
//    * */
//    @GetMapping(value="/getUserInfo")
//    public Result<SysUser> getUserInfo(@RequestHeader("token") String token){
//
//         SysUser sysUser = sysUserService.getUserInfo(token);
//
//        return Result.build(sysUser,ResultCodeEnum.SUCCESS);
//    }
    //折磨 ！！
        @Operation(summary = "从ThreadLocal对象中获取用户信息")
        @GetMapping(value = "/getUserInfo")
        public Result<SysUser> getUserInfo() {
            return Result.build(AuthContextUtil.get(), ResultCodeEnum.SUCCESS) ;
        }

    /*
    *
    * 退出登录的方法
    * */
    @GetMapping(value = "/logout")
    public Result logout(@RequestHeader("token") String token){
        sysUserService.logout(token);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }


}
